<template>
  <div class="app-container">
    <el-form label-width="120px">
      <el-form-item>
        <el-tag type="info">excel模板说明</el-tag>
        <el-tag>
          <i class="el-icon-download"></i>
          <a :href="'/static/test.xlsx'" download>点击下载模板</a>
        </el-tag>
      </el-form-item>
      <el-form-item label="选择Excel">
        <el-upload
          class="upload-demo"
          ref="upload"
          :action="BASE_API + '/eduservice/subject'"
          :on-success="fileUploadSuccess"
          :on-error="fileUploadError"
          :auto-upload="false"
          :limit="1"
          :disabled="importBtnDisabled"
          name="file"
          accept=".xlsx"
        >
          <el-button slot="trigger" size="small" type="primary"
            >选取文件</el-button
          >
          <el-button
            :loading="loading"
            style="margin-left: 10px;"
            size="small"
            type="success"
            @click="submitUpload"
            >上传到服务器</el-button
          >
          <div slot="tip" class="el-upload__tip">
            只能上传jpg/png文件，且不超过500kb
          </div>
        </el-upload>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      BASE_API: process.env.VUE_APP_BASE_API,
      importBtnDisabled: false,
      loading: false
    }
  },
  methods: {
    submitUpload() {
      this.$refs.upload.submit()
    },
    // 上传成功
    fileUploadSuccess() {
      this.loading = false
      this.$message({
        type: 'success',
        message: '添加课程分类成功'
      })

      // 跳转到列表
      this.$router.push('/edu/subject/list')
    },
    // 上传失败
    fileUploadError() {
      this.loading = false
      this.$message({
        type: 'error',
        message: '导入失败'
      })
    }
  }
}
</script>
